﻿/**************************************************************************************************\
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++
The Software is protected by US copyright laws and international treaties. Licensee must treat the 
Software like any other copyrighted material – for example a book. Licensee acknowledges that 
Arkadium owns the Software and that nothing set forth herein conveys to Licensee any ownership in 
or to the Software, including without any Derivative Works of the Software.  
++++++++++++++++++++++++++++++ ARKADIUM INTELLECTUAL PROPERTY TERMS ++++++++++++++++++++++++++++++

AUTHOR: Neil Mrva

PURPOSE: This class is a sort of XML Delegate class which extends the XML object that encapsulates 
the basic setup needed to load an XML file. Typically when we needed to load an XML file, we needed 
to create an XML file, ignore white space, load file, and finally setup an onLoad listener which 
didn't maintain scope unless we used Delegate. But by using Delegate we needed to manually keep 
track of the XML object to parse once it loaded. This class takes care of all that and calls back 
the calling objects' listener and provides itself as the XML object for parsing.  
\**************************************************************************************************/

import mx.utils.Delegate;

class arkadium.XMLFile extends XML
{
	private var _fCallBackFunction:Function;

	/**************************************************************************************************\
	XMLFile - Constructor used to set up the all to common ignoreWhite variable.
	\**************************************************************************************************/
	public function XMLFile()
	{
		this.ignoreWhite = true;		
		_fCallBackFunction = null;
	}
	
	/**************************************************************************************************\
	loadFile - Takes the calling object and it's listener function to callback once the XML file loaded.
	Besides that additional functionality - it's just like calling load() with XML.
	\**************************************************************************************************/
	public function loadFile( oCallerObject:Object, fCallerFunction:Function, sFileURL:String )
	{
		_fCallBackFunction = Delegate.create( oCallerObject, fCallerFunction );
		this.load( sFileURL );
	}
	
	/**************************************************************************************************\
	onLoad - Called once the XML file has loaded and will notify the listener of the event with a 
	reference to itself and whether the load was successful or not.
	\**************************************************************************************************/
	private function onLoad( bSuccess:Boolean )
	{
		_fCallBackFunction.apply( null, [ this, bSuccess ] );
	}
	
	public function parseVarsToNewObjects(parentObject:Object){
		loadXMLVarsToObject(this.firstChild,parentObject)
	}
		
	public function loadXMLVarsToObject(xValues,oSaveTo:Object){
		if (xValues.firstChild.firstChild != undefined) {
			var newObject:Object;
			
			if(oSaveTo["set"+xValues.nodeName+"FromXML"] == undefined) newObject = oSaveTo;
			else newObject = oSaveTo["set"+xValues.nodeName+"FromXML"]();
			
			var aChildren = xValues.childNodes;
			var num:Number = 0;
			trace (aChildren[num].attributes.toString ())
			while(num<100 && aChildren[num]!=undefined ){ 
				if(aChildren[num].firstChild.firstChild != undefined){
					loadXMLVarsToObject(aChildren[num],newObject);
				}else {
					if (aChildren[num].attributes.type == "Number"){
						newObject[aChildren[num].nodeName] =  Number(aChildren[num].firstChild.nodeValue);
					}
					else if (aChildren[num].attributes.type == "Boolean"){
						newObject[aChildren[num].nodeName] =  Boolean(Number(aChildren[num].firstChild.nodeValue));
					}
					else if(aChildren[num].attributes.type == "Array"){
						newObject[aChildren[num].nodeName] = new Array();
						newObject[aChildren[num].nodeName] = aChildren[num].firstChild.nodeValue.split(",");
					}
					else newObject[aChildren[num].nodeName] =  aChildren[num].firstChild.nodeValue;
				}
				num++;
				
			}
			if(newObject != oSaveTo)oSaveTo["_a"+xValues.nodeName].push(newObject);
		}
		for (var i in oSaveTo)
		{
			for (var j in oSaveTo[i])
			{
				trace (i + "." + j + " = " + oSaveTo[i][j]);
				trace ("anidado")
			}
			
			trace (i + " = " + oSaveTo[i]);
			
		}
	}
	
}